﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgriculturalMachinery.Models;

namespace AgriculturalMachinery.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index(int? page)
        {
            // return View(new ProductViewModel(1));
            ProductRepository pr = new ProductRepository();
            var AllProduct = pr.FindAllProducts();
            var PagedAllProduct = new ProductViewModel<ProductInfo>(AllProduct, page ?? 0, 7);
            return View(PagedAllProduct);
        }

        public ActionResult Browse()
        {
            AccountRepository ar = new AccountRepository();
            ViewData["AllClients"] = ar.FindAllClients();

            ProductRepository pr = new ProductRepository();
            ViewData["AllProduct"] = pr.FindAllProducts();
            return View();
        }
        //
        // GET: /Product/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Product/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                string GearboxModel = Request.Params["GearboxModel"];
                string EngineModel = Request.Params["EngineModel"];
                // 如果变速箱型号不为空, 发动机型号为空
                // 说明添加的是变速箱
                if (GearboxModel != null && EngineModel == null)
                {
                    int GearboxNameplate = Convert.ToInt32(Request.Params["GearboxNameplate"]);
                    string GearboxFactory = Request.Params["GearboxFactory"];
                    DateTime GearboxSotckDate = DateTime.Now;
                    int GearboxMargin = Convert.ToInt32(Request.Params["GearboxMargin"]);
                    int GearboxPrice = Convert.ToInt32(Request.Params["GearboxPrice"]);

                    ProductRepository pr = new ProductRepository();
                    ProductInfo p = new ProductInfo();
                    p.GearboxModel = GearboxModel;
                    p.GearboxNameplate = GearboxNameplate;
                    p.GearboxFactory = GearboxFactory;
                    p.GearboxStockDate = DateTime.Now;
                    p.GearboxMargin = GearboxMargin;
                    p.GearboxPrice = GearboxPrice;

                    pr.addProduct(p);
                    return RedirectToAction("Index");
                }
                else if (EngineModel != null && GearboxModel == null)
                {
                    // 如果发动机型号不为空, 变速箱型号为空
                    // 说明添加的是发动机
                    int EngineNameplate = Convert.ToInt32(Request.Params["EngineNameplate"]);
                    string EngineFactory = Request.Params["EngineFactory"];
                    DateTime EngineSotckDate = DateTime.Now;
                    int EngineMargin = Convert.ToInt32(Request.Params["EngineMargin"]);
                    int EnginePrice = Convert.ToInt32(Request.Params["EnginePrice"]);

                    ProductRepository pr2 = new ProductRepository();
                    ProductInfo p2 = new ProductInfo();
                    p2.EngineModel = EngineModel;
                    p2.EngineNameplate = EngineNameplate;
                    p2.EngineFactory = EngineFactory;
                    p2.EngineSotckDate = EngineSotckDate;
                    p2.EngineMargin = EngineMargin;
                    p2.EnginePrice = EnginePrice;

                    pr2.addProduct(p2);
                    return RedirectToAction("index");
                }
                return RedirectToAction("index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Delete/5

        public ActionResult Delete(int id)
        {
            ProductRepository pr = new ProductRepository();
            // 根据一个id删除一个产品
            pr.deleteProduct(id);
            return RedirectToAction("Index");
        }

        //
        // POST: /Product/Delete/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                string[] delSting = Request.Params["delString"].Split('|');
                ProductRepository pr = new ProductRepository();
                for (int i = 0; i < delSting.Length; i++)
                {
                    pr.deleteProduct(Convert.ToInt32(delSting[i]));
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
